#define _CRT_SECURE_NO_WARNINGS
#define HPDataType int
#include <stdio.h>
void Swap(HPDataType* arr, int x, int y) {
	HPDataType tmp;
	tmp = arr[x];
	arr[x] = arr[y];
	arr[y] = tmp;
}
void AdjustUp(HPDataType* arr, int child) {
	int parent = (child - 1) / 2;
	while (child != 0 && arr[child] > arr[parent]) {
		Swap(arr, child, parent);
		child = parent;
		parent = (child - 1) / 2;
	}
}
void AdjustDown(HPDataType* arr, int parent, int size) {
	int child = 1;
	while (child <= size - 1) {
		if (child < size - 1 && arr[child] < arr[child + 1])
			child++;
		if (arr[parent] < arr[child])
			Swap(arr, child, parent);
		parent = child;
		child = parent * 2 + 1;
	}
}
void Heap_Sort(HPDataType* arr, int sz) {
	int i = 0;
	for (i = (sz - 2) / 2; i >= 0; i--)
		AdjustDown(arr, i, sz);
	i = sz - 1;
	while (i) {
		Swap(arr, 0, i);
		AdjustDown(arr, 0, i);
		i--;
	}
}
int main() {
	int arr[7] = { 46,36,22,57,48,26,19 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	Heap_Sort(arr, sz);

	return 0;
}